﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.ComponentModel;
using System.Collections.Specialized;
using MacomberMapSystem.Common.Internals;

namespace MacomberMapSystem.Common.User_Interfaces
{
    /// <summary>
    /// This class handles email sending
    /// </summary>
    public static class MM_Email_Sending
    {
        /// <summary>
        /// Send an email 
        /// </summary>
        /// <param name="SMTPServer">The SMTP server</param>
        /// <param name="FromAddress"></param>
        /// <param name="FromName"></param>
        /// <param name="Subject"></param>
        /// <param name="Recipients"></param>
        /// <param name="CarbonCopies"></param>
        /// <param name="BlindCarbonCopies"></param>
        /// <param name="Message"></param>
        /// <param name="Attachments"></param>
        public static void SendEmail(String SMTPServer, String FromAddress, String FromName, String Subject, MM_String_Collection Recipients, MM_String_Collection CarbonCopies, MM_String_Collection BlindCarbonCopies, String Message, params Object[] Attachments)
        {
            SendEmail(SMTPServer, FromAddress, FromName, Subject, Recipients, CarbonCopies, BlindCarbonCopies, Message, false, true, Attachments);
        }        

        /// <summary>
        /// Send an email
        /// </summary>
        /// <param name="SMTPServer"></param>
        /// <param name="FromAddress"></param>
        /// <param name="FromName"></param>
        /// <param name="Subject"></param>
        /// <param name="Recipients"></param>
        /// <param name="CarbonCopies"></param>
        /// <param name="BlindCarbonCopies"></param>
        /// <param name="Message"></param>
        /// <param name="IsHtml"></param>
        /// <param name="Asynch"></param>
        /// <param name="Attachments"></param>
        public static void SendEmail(String SMTPServer, String FromAddress, String FromName, String Subject, MM_String_Collection Recipients, MM_String_Collection CarbonCopies, MM_String_Collection BlindCarbonCopies, String Message, bool IsHtml, bool Asynch, params Object[] Attachments)
        {
            //Create our SMTP serssion and message
            SmtpClient Session = new SmtpClient(SMTPServer);
            MailMessage OutMessage = new MailMessage();

            //Set our from, subject & recipients
            OutMessage.From = new MailAddress(FromAddress, FromName);
            if (Recipients != null)
                foreach (String Recipient in Recipients)
                    OutMessage.To.Add(new MailAddress(Recipient));
            if (CarbonCopies != null)
                foreach (String CarbonCopy in CarbonCopies)
                    if (!String.IsNullOrEmpty(CarbonCopy))
                        OutMessage.CC.Add(new MailAddress(CarbonCopy));
            if (BlindCarbonCopies != null)
                foreach (String BlindCarbonCopy in BlindCarbonCopies)
                    if (!String.IsNullOrEmpty(BlindCarbonCopy))
                        OutMessage.CC.Add(new MailAddress(BlindCarbonCopy));

            //Set the subject and body
            OutMessage.Subject = Subject;
            OutMessage.IsBodyHtml = IsHtml;
            OutMessage.Body = Message;
            foreach (Object Attach in Attachments)
                if (Attach is String)
                    OutMessage.Attachments.Add(new Attachment((string)Attach));                
                else if (Attach is Attachment)
                    OutMessage.Attachments.Add(Attach as Attachment);

            
            //Configure the SMTP session, and send out the message
            Session.ServicePoint.MaxIdleTime = 1000;

            if (Asynch)
            {
                Session.SendCompleted += new SendCompletedEventHandler(Session_SendCompleted);
                Session.SendAsync(OutMessage, Message);
            }
            else
                Session.Send(OutMessage);
        }

        /// <summary>
        /// Handle the completion of the message
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Session_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //   (e.UserState as MailMessage).Dispose();
        }

    }
}
